package com.cskaoyan.catmall.controller.background;

import com.cskaoyan.catmall.annotation.OperationLog;
import com.cskaoyan.catmall.model.bo.CateGoryCreateBo;
import com.cskaoyan.catmall.model.bo.CateGoryUpdateBo;
import com.cskaoyan.catmall.model.bo.CategoryDeleteBo;
import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.model.vo.CategoryL1Vo;
import com.cskaoyan.catmall.model.vo.CategoryListVo;
import com.cskaoyan.catmall.service.CateGoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 创建日期: 2022-01-07-21-42
 * author : WQW
 * description: 商品类目相关接口
 */
@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    CateGoryService cateGoryService;


    /**
     * @param
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 获取商品类目信息
     * @author WQW
     * @date 2022/1/8 10:09
     */

    @OperationLog(value = "查询商品类目")
    @RequiresPermissions("admin:category:list")
    @GetMapping("list")
    public BaseRespVo list() {
        CategoryListVo categoryListVo = cateGoryService.getCategoryList();
        return BaseRespVo.ok(categoryListVo);
    }


    /**
     * @param
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 获取一级类目信息
     * @author WQW
     * @date 2022/1/9 10:48
     */

    @OperationLog(value = "查询一级类目")
    @GetMapping("l1")
    public BaseRespVo l1() {
        CategoryL1Vo categoryL1Vo = cateGoryService.getCategoryL1List();
        return BaseRespVo.ok(categoryL1Vo);
    }

    /**
     * @param
     * @param cateGoryCreateBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 添加商品类目信息
     * @author WQW
     * @date 2022/1/7 22:26
     */

    @OperationLog(value = "添加商品类目")
    @RequiresPermissions("admin:category:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody @Validated CateGoryCreateBo cateGoryCreateBo,BindingResult bindingResult) {
        int result = cateGoryService.addCateGory(cateGoryCreateBo);
        if (result == 200) {
            return BaseRespVo.ok(cateGoryCreateBo);
        }
        return BaseRespVo.fail("添加商品类目失败，请稍后重试");
    }


    /**
     * @param
     * @param categoryDeleteBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 删除商品类目信息
     * @author WQW
     * @date 2022/1/8 10:08
     */

    @OperationLog(value = "删除类目")
    @RequiresPermissions("admin:category:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody CategoryDeleteBo categoryDeleteBo) {
        int result = cateGoryService.deleteCateGory(categoryDeleteBo);
        if (result == 200) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("删除类目信息失败，请稍后重试");
    }


    /**
     * @param
     * @param cateGoryUpdateBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 修改商品类目信息
     * @author WQW
     * @date 2022/1/8 10:08
     */

    @OperationLog(value = "修改类目")
    @RequiresPermissions("admin:category:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody @Validated CateGoryUpdateBo cateGoryUpdateBo, BindingResult bindingResult) {
        int result = cateGoryService.updateCateGory(cateGoryUpdateBo);
        if (result == 200) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("修改类目信息失败，请稍后重试");
    }

}
